<?php

global $WSC;

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_web_page_wed.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_template_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_ci_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_category_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpt_menu_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_item_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_system_attribute_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpt_system_attribute_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/nomenclature/tcms_nomenclature_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/ci_attr_types/tcms_cia_nomenclature_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_image_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_attachment_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/image/tcms_image_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/attachment/tcms_attachment_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_images_dbc.class.php');


abstract class TCMS_VPT_Page_WP extends TCMS_Web_Page_Wed {
	private $vpage;
	private $vpt;
	private $settings_visuals;

	function __construct(TCMS_VPage_DBC $vpage, TCMS_VPage_Template_DBC $vpt, $page_name) {
		$this->vpage = $vpage;
		$this->vpt = $vpt;
		parent::__construct($vpt->get_page_name());
	}

	
	/**
	 * @return TCMS_VPage
	 */
	public function get_vpage() {
		return $this->vpage;
	}


	public function get_vpt() {
		return $this->vpt;
	}


	public function get_settings_visuals() {
		return $this->settings_visuals;
	}


	public function init() {
		parent::init();

		$dbc = $this->get_dbc();
		$settings_visuals = new TCMS_Settings_Visuals_DBC();
		if ($settings_visuals->load_by_id($dbc, 1)) {
			$this->settings_visuals = $settings_visuals;
			$this->export('settings_visuals', $settings_visuals);
		} else {
			throw new Tangra_Module_Exception('Cannot load settings visuals');
		}

		$v = $this->create_view();
		$this->add_view($v);
	}


	protected function default_event() {
		$dbc = $this->get_dbc();

		$categories = TCMS_VPage_Category_DBC::load_vpage_categories($dbc, $this->vpage->get_id());
		$this->export('categories', $categories);


		$this->export_system_attributes($this->vpage);

		$menus = TCMS_VPT_Menu_DBC::load_all_for_template($dbc, $this->vpage->get_template());
		foreach($menus as $menu) {
			$menu_items = TCMS_Menu_Item_DBC::load_menu_items_full($dbc, $menu->get_id(), $this->_config->get_hidden_dir().'inc/', true);
			$this->export($menu->get_hid(), $menu_items);
		}

		$view = $this->get_view('default');

		return $view;
	}


	protected function export_system_attributes($vpage) {
		$dbc = $this->get_dbc();

		$t_sas = TCMS_VPT_System_Attribute_DBC::select_vpt_sas_for_template($dbc, $this->vpage->get_template());
		$page_system_attributes = TCMS_VPage_System_Attribute_DBC::load_all_for_vpage($dbc, $this->vpage->get_id());

		$config = $this->get__config();
		foreach($page_system_attributes as $t_sas_id => $sa) {
			if ($t_sas[$t_sas_id]['export']) {
				$this->export($t_sas[$t_sas_id]['export_name'], htmlentities($sa->get_value(), NULL, $config->get_default_encoding()));
			}
		}
	}


	protected function export_attachments(DB_Connection $dbc, $ci_id, $export_prefix = '') {
		$attachments = $this->load_attachments_for_ci($dbc, $ci_id);
		$this->export($export_prefix.'attachments', $attachments);
	}


	protected function export_images(DB_Connection $dbc, $ci_id, $export_prefix = '') {
		$images = $this->load_images_for_ci($dbc, $ci_id);
		$this->export($export_prefix.'images', $images);

		$settings_images = TCMS_Settings_Images_DBC::load_settings_flat($this->get_dbc());
		$this->export('t1_width', $settings_images['thumbnail1_size_x']);
		$this->export('t1_height', $settings_images['thumbnail1_size_y']);
		$this->export('t2_width', $settings_images['thumbnail2_size_x']);
		$this->export('t2_height', $settings_images['thumbnail2_size_y']);
	}


	public static function load_attachments_for_ci(DB_Connection $dbc, $ci_id) {
		tangra_if_not_int_throw_e($ci_id);

		$ci_attachments = TCMS_CI_Attachment_DBC::load_ci_attachments($dbc, $ci_id);
		$attachments = array();
		foreach ($ci_attachments as  $ci) {
			$tmp = new TCMS_Attachment_DBC();
			if ($tmp->load_by_id($dbc, $ci->get_attachment())) {
				$attachments[] = clone $tmp;
			}
		}
		return $attachments;
	}


	public static function load_images_for_ci(DB_Connection $dbc, $ci_id) {
		tangra_if_not_int_throw_e($ci_id);

		$ci_images = TCMS_CI_Image_DBC::load_ci_images($dbc, $ci_id);
		$images = array();
		foreach ($ci_images as  $cii) {
			$tmp = new TCMS_Image_DBC();
			if ($tmp->load_by_id($dbc, $cii->get_image())) {
				$images[] = clone $tmp;
			}
		}

		return $images;
	}


	public static function get_short_text_description(DB_Connection $dbc, $vpage_id) {
		return '';
	}


	public function create_view($name = 'default') {
		$dbc = $this->get_dbc();


		$ret = new TCMS_Web_Page_View($this, $name, $this->settings_visuals->get_html_resource_path().'pages/');

		return $ret;
	}


	public static function new_created(DB_Connection $dbc, TCMS_VPage_DBC $vpage) {
		// empty
	}


	public function get_vpage_last_modified() {
		return $this->get_vpage()->get_last_modified();
	}
}